﻿using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using EmitMapper;
using EmitMapper.MappingConfiguration;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.JsonModel;
using WebPlatform.FiltersAndSession;
using WebPlatform.Models;

namespace WebPlatform.Controllers
{
    [CustomAuthorize]
    public class ContentController : Controller
    {
        public IManagerContent IMContent { get; set; }

        /// <summary>
        /// 登录会话操作
        /// </summary>
        public Sessions Sessions { get; set; }

        public ActionResult Index(string articlePartitionId)
        {
            if (!string.IsNullOrEmpty(articlePartitionId))
            {
                Sessions.ArticlePartition = articlePartitionId;
            }
            // ArticleInfo ai = new ArticleInfo();
            // ai.ReleaseDate = DateTime.Now;
            // ai.AdminUserId = 1;
            // ai.AdminUserName = "aa";
            // ai.CompanyID = 1;
            // ai.PartitionId = "GG";
            // ai.Title = "test";
            // ai.ArticleDetail = new ArticleDetailInfo() { Content ="test2"};

            //IMContent.AddContent(ai);
            //var i= IMContent.LoadAll(e=>true,1,10, new OrderByExpression<ArticleInfo,DateTime>(a => a.ReleaseDate));
            return View();
        }

        [HttpPost]
        public JsonResult Contents(int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;
            var partition = Sessions.ArticlePartition;

            GridData<ArticleVM> data = new GridData<ArticleVM>();
            ObjectsMapper<ArticleInfo, ArticleVM> mapper =
                   ObjectMapperManager.DefaultInstance.GetMapper<ArticleInfo, ArticleVM>();

            var where = PredicateExtension.True<ArticleInfo>();
            where = where.And(u => u.CompanyID == user.CompanyId);
            where = where.And(u => u.PartitionId == partition);

            var count = IMContent.LoadCount(where);
            var list = IMContent.LoadAll(where, page, pageSize, new OrderByExpression<ArticleInfo, DateTime>(u => u.ReleaseDate, true));
            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }

        public ActionResult Edit(int Id = 0)
        {
            ArticleVM article = new ArticleVM();
            ObjectsMapper<ArticleInfo, ArticleVM> mapper =
                   ObjectMapperManager.DefaultInstance.GetMapper<ArticleInfo, ArticleVM>();

            if (Id > 0)
            {
                var res = IMContent.GetArticle(Id);
                article = mapper.Map(res);
                article.Content = res.ArticleDetail.Content;
            }
            if (string.IsNullOrEmpty(article.ImgUrl))
            {
                article.ImgUrl = "/Images/nothing.jpg";
            }
            var partitionId = Sessions.ArticlePartition;
            ViewBag.Partition = partitionId;

            switch (partitionId)
            {
                case "HSZZXWPIC":
                    ViewBag.Size = "图片尺寸：宽*高（800*600）";
                    break;
                case "HSZZFMPIC":
                    ViewBag.Size = "图片尺寸：宽*高（530*760）";
                    break;
                default:
                    ViewBag.Size = "";
                    break;
            }
            return View(article);
        }


        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Edit(ArticleVM av)
        {
            if (ModelState.IsValid)
            {
                ObjectsMapper<ArticleVM, ArticleInfo> mapper =
                  ObjectMapperManager.DefaultInstance.GetMapper<ArticleVM, ArticleInfo>();

                var user = Sessions.User;
                var partition = Sessions.ArticlePartition;
                if (av.ContentId == 0)
                {
                    var a = mapper.Map(av);
                    a.AdminUserId = user.UserID;
                    a.AdminUserName = user.UserName;

                    a.CompanyID = user.CompanyId;
                    a.PartitionId = partition;
                    a.ReleaseDate = DateTime.Now;
                    a.ArticleDetail = new ArticleDetailInfo() { Content = av.Content };
                    bool res = IMContent.AddContent(a);
                    return Json(res, "text/html; charset=UTF-8");
                }
                else
                {
                    var a = IMContent.GetArticle(av.ContentId);
                    a.ArticleDetail.Content = av.Content;
                    a.ArticleDetail.ContentId = av.ContentId;
                    a.ArticleDetail.Article = a;
                    a.AdminUserId = user.UserID;
                    a.AdminUserName = user.UserName;
                    a.ImgUrl = av.ImgUrl;
                    a.Title = av.Title;
                    a.CompanyID = user.CompanyId;
                    a.PartitionId = partition;
                    a.ReleaseDate = DateTime.Now;
                    a.Numbers = av.Numbers;

                    bool res = IMContent.UpdateContent(a);
                    return Json(res, "text/html; charset=UTF-8");
                }

            }
            else
            {
                return Json(false, "text/html; charset=UTF-8");
            }
        }


        [HttpPost]
        public JsonResult DeleteContents(string ids)
        {
            var b = false;
            if (!string.IsNullOrEmpty(ids))
            {
                var idList = ids.Split(',');
                var list = new List<int>();
                foreach (var item in idList)
                {
                    list.Add(Convert.ToInt32(item));
                }

                b = IMContent.DeleteContent(list);
            }
            return Json(b, "text/html; charset=UTF-8");
        }

    }
}
